package edu.xpu.project.user.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import edu.xpu.project.excel.help.ParseExcel;
import edu.xpu.project.user.entity.ExamPlaceEntity;
import edu.xpu.project.user.entity.ProjectUser;
import edu.xpu.project.user.entity.StudentClass;
import edu.xpu.project.user.service.ExamPlaceService;
import edu.xpu.project.user.service.ProjectUserService;
import edu.xpu.project.user.service.StudentClassService;

/**
 * 系统用户控制器
 * @author liukang
 *
 */
@Controller
@RequestMapping(value="/user")
public class ProjectUserController {
	
	private static Set<String> randSet = new CopyOnWriteArraySet<String>();	
	
	@Autowired
	private ProjectUserService projectUserService;
	@Autowired
	private StudentClassService studentClassService;
	@Autowired
	private ExamPlaceService examPlaceService;
	
	
	/**
	 * @describe 导入学生excel表
	 * @date 2017年5月20日
	 * @author liukang
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value="/excelImport")
	public String excelImport(@RequestParam(value="file") MultipartFile multipartFile, @RequestParam(value="randNum") String randNum){
		//获取randList, 和随机数做对比
		int sizeOld = randSet.size();
		randSet.add(randNum);
		int sizeNew = randSet.size();
		if (sizeOld == sizeNew) {
			return "{\"status\":\"multi\"}";
		}
		
		//接受excel表格 , 并进行分析存库
		//String originalFilename = multipartFile.getOriginalFilename();
		//获取文件流
		InputStream inputStream = null;
		/*
		 * 由于这块保存需要使用将近5分钟, 因此考虑使用级联保存
		 */
		try {
			inputStream = multipartFile.getInputStream();
			ParseExcel parseExcel = new ParseExcel();
			List<ProjectUser> readExcelContentForStudentTab = parseExcel.readExcelContentForStudentTab(inputStream);
			//存入数据库
			for (ProjectUser projectUser : readExcelContentForStudentTab) {
				//存班级
				StudentClass studentClass = projectUser.getStudentClass();
				studentClass = studentClassService.saveOrUpdate(studentClass);
				//存考场
				ExamPlaceEntity examPlace = projectUser.getExamPlace();
				examPlace = examPlaceService.saveOrUpdate(examPlace);
				//存学生
				projectUser.setStudentClass(studentClass);
				//登录名和密码默认
				//projectUser.setLoginName(loginName);
				projectUser.setExamPlace(examPlace);
				projectUserService.saveOrUpdate(projectUser);
			}
		} catch (Exception e) {
			return "{\"status\":\"error\"}";
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e1) {}
			}
		}
		return "{\"status\":\"sucess\"}";
	}
	
	
	
	
	
	
	@RequestMapping(value="toTeacherAdd")
	public String toTeacherAdd() {
		
		return "teacher/teacherAdd";
	}
	
	/**
	 * @describe 学生信息保存
	 * @date 2017年4月18日
	 * @author liukang
	 */
	@RequestMapping(value="/studentAdd")
	public String studentAdd(ProjectUser projectUser, ModelMap modelMap) {
		//添加系统属性
		projectUser.setCreateDate(new Date());
		projectUserService.saveOrUpdate(projectUser);
		modelMap.addAttribute("userType", projectUser.getUserType());
		return "redirect:studentList";
	}
	
	/**
	 * @describe 去添加学生信息页面
	 * @date 2017年4月18日
	 * @author liukang
	 */
	@RequestMapping(value="/toStudentAdd")
	public String toStudentAdd() {
		
		return "student/userAdd";
	}
	
	/**
	 * 用户列表展示
	 * @param pageNumber
	 * @param pageSize
	 * @param projectUser
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/studentList")
	public String studentList(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			ProjectUser projectUser, ModelMap modelMap) {
		Page<ProjectUser> pageByCriterias = projectUserService.getPageByCriterias(pageNumber, pageSize, projectUser);
		//将各个参数传到前端
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("searchVal", projectUser)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize);
		if (projectUser.getUserType() == 1) {		//学生
			return "student/userList";
		} else if (projectUser.getUserType() == 2) {	//老师
			return "teacher/teacherList";
		}
		return null;
	}
	
}
